/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import rules.Rule;
import types.MatrixApplicableRules;

/**
 *
 * @author ali
 */
public class GuiHelper {

    public static int getDifMinMaxApplicableRulesOfWholeBoard(MatrixApplicableRules mar) {
        boolean firstEmptyMet = false;
        int min = 0;
        int max = 0;
        Collection apr = null;
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                apr = mar.getApplicableRules(i, j);
                GuiHelper.sortColOfRulesByWeight(apr);
                

                if (apr.size() > 0) {
                    Iterator iter = apr.iterator();
                    Rule rl = (Rule) iter.next();
                    if (!firstEmptyMet) {
                        min = rl.getWeight(rl.getQuadrat());
                        max = min;
                        firstEmptyMet = true;
                    } else {
                        if (rl.getWeight(rl.getQuadrat())<min)
                            min = rl.getWeight(rl.getQuadrat());
                        if (rl.getWeight(rl.getQuadrat())>max)
                            max = rl.getWeight(rl.getQuadrat());
                    }
                }
            }

        }
        System.out.println("max-min =" +(max-min));
        return max - min;
    }

    public static int getMinApplicableRulesOfWholeBoard(MatrixApplicableRules mar) {
        boolean firstEmptyMet = false;
        int min = 0;
        Collection apr = null;
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                apr = mar.getApplicableRules(i, j);
                GuiHelper.sortColOfRulesByWeight(apr);
                

                if (apr.size() > 0) {
                    Iterator iter = apr.iterator();
                    Rule rl = (Rule) iter.next();
                    if (!firstEmptyMet) {
                        min = rl.getWeight(rl.getQuadrat());
                        firstEmptyMet = true;
                    } else {
                        if (rl.getWeight(rl.getQuadrat())<min)
                            min = rl.getWeight(rl.getQuadrat());
                    }
                }
            }

        }
        System.out.println("min = " +min);
        return min;
    }
    public static int getMaxApplicableRulesOfWholeBoard(MatrixApplicableRules mar) {
        boolean firstEmptyMet = false;
        int max = 0;
        Collection apr = null;
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                apr = mar.getApplicableRules(i, j);
                GuiHelper.sortColOfRulesByWeight(apr);


                if (apr.size() > 0) {
                    Iterator iter = apr.iterator();
                    Rule rl = (Rule) iter.next();
                    if (!firstEmptyMet) {
                        max = rl.getWeight(rl.getQuadrat());
                        firstEmptyMet = true;
                    } else {
                        if (rl.getWeight(rl.getQuadrat())>max)
                            max = rl.getWeight(rl.getQuadrat());
                    }
                }
            }

        }
        System.out.println("max-min =" +max);
        return max;
    }
    public static void sortColOfRulesByWeight(Collection col) {
        Collection sortedC = new ArrayList();

        Collections.sort((ArrayList<Rule>) col);

    }

    public static int calculateX(int block, int field) {
        if (block >= 0 && block <= 2) {
            if (field >= 0 && field <= 2) {
                return 0;
            } else if (field >= 3 && field <= 5) {
                return 1;
            } else {
                return 2;
            }
        } else if (block >= 3 && block <= 5) {
            if (field >= 0 && field <= 2) {
                return 3;
            } else if (field >= 3 && field <= 5) {
                return 4;
            } else {
                return 5;
            }
        } else {
            if (field >= 0 && field <= 2) {
                return 6;
            } else if (field >= 3 && field <= 5) {
                return 7;
            } else {
                return 8;
            }
        }

    }

    public static int calculateY(int block, int field) {
        if (block == 0 || block == 3 || block == 6) {
            if (field == 0 || field == 3 || field == 6) {
                return 0;
            } else if (field == 1 || field == 4 || field == 7) {
                return 1;
            } else {
                return 2;
            }
        } else if (block == 1 || block == 4 || block == 7) {
            if (field == 0 || field == 3 || field == 6) {
                return 3;
            } else if (field == 1 || field == 4 || field == 7) {
                return 4;
            } else {
                return 5;
            }
        } else {
            if (field == 0 || field == 3 || field == 6) {
                return 6;
            } else if (field == 1 || field == 4 || field == 7) {
                return 7;
            } else {
                return 8;
            }
        }

    }
}
